package org.geektimes.util;

import java.io.*;

/**
 * 递归读取某个文件夹下的所有html文件，
 * 删除里面的  -webkit-line-clamp:5;   字符串
 */
public class FileUtil {

    private final static String replace = "-webkit-line-clamp:5;";
    private final static String directPath = "H:\\D盘文件\\xxx";

    public static void main(String[] args) throws IOException {
        FileUtil.readFile(directPath);
    }

    public static void readFile(String path) throws IOException {
        final File file = new File(path);
        if(file.isDirectory()){
            final File[] files = file.listFiles();
            for (File file1 : files) {
                if (file1.isDirectory()){
                    readFile(file1.getPath());
                }else if(file1.getName().endsWith(".html")){
                    System.out.println(file1.getName());
                    changeFile(file1);
                }
            }
        }else if(file.getName().endsWith(".html")){
            System.out.println(file.getName());
            changeFile(file);
        }
    }

    //删除 -webkit-line-clamp:5;
    public static void changeFile(File file) throws IOException {
        final FileReader fileReader = new FileReader(file);
        final BufferedReader bufferedReader = new BufferedReader(fileReader);

        File temp = new File(directPath+File.separator+file.getName());
        FileWriter fileWriter = new FileWriter(temp);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        Boolean finded = false;
        String str;
        while((str = bufferedReader.readLine()) != null){
            if(!finded&&str.contains(replace)){
                str = str.replace(replace,"");
                finded = true;
            }
            bufferedWriter.write(str);
        }

        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();
        bufferedReader.close();
        fileReader.close();
    }

}
